package com.cby.library.common.base

import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.cby.library.common.R
import com.cby.library.common.base.loadmore.CustomLoadMoreView
import com.cby.library.common.ext.observe
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * @description: BasePageStateActivity
 *
 * @author ChenBaoyang
 * @date 4/20/21 16:15
 */
abstract class BasePageStateActivity<VM : BasePageViewModel<T>, T> : BaseStateActivity<VM>() {

    private lateinit var refreshLayout: SwipeRefreshLayout
    private lateinit var recyclerView: RecyclerView

    private lateinit var dataAdapter: BaseQuickAdapter<T, BaseViewHolder>

    override fun bindLiveData() {
        observe(viewModel!!.data) {
            refreshLayout.isRefreshing = false
            dataAdapter.loadMoreModule.isEnableLoadMore = true
            dataAdapter.loadMoreModule.isAutoLoadMore = true
            if (it.page == 1) {
                dataAdapter.setNewData(it.data as MutableList<T>?)
                dataAdapter.loadMoreModule.loadMoreComplete()
            } else {
                if(it.data!!.isEmpty()) {
                    dataAdapter.loadMoreModule.loadMoreEnd()
                    dataAdapter.loadMoreModule.isEnableLoadMore = false
                }else{
                    dataAdapter.addData(it.data!!)
                    dataAdapter.loadMoreModule.loadMoreComplete()
                }
            }

        }
    }

    override fun onDataInit() {

    }

    override fun initView() {
        refreshLayout = findViewById(R.id.swipeRefreshLayout)
        recyclerView = findViewById(R.id.recyclerView)

        dataAdapter = getAdapter()

        dataAdapter.loadMoreModule.isEnableLoadMore = false
        dataAdapter.loadMoreModule.isAutoLoadMore = true
        dataAdapter.loadMoreModule.isEnableLoadMoreIfNotFullPage = false
        dataAdapter.loadMoreModule.loadMoreView = CustomLoadMoreView()

        addHeaderView(dataAdapter)

        dataAdapter.loadMoreModule.setOnLoadMoreListener {
            loadMoreData()
        }

        val linearLayoutManager = LinearLayoutManager(this)
        recyclerView.apply {
            layoutManager = linearLayoutManager
            adapter = dataAdapter
            itemAnimator = DefaultItemAnimator()
        }

        refreshLayout.setOnRefreshListener {
            loadData()
        }


    }

    private fun loadData() {
        dataAdapter.loadMoreModule.isEnableLoadMore = false
        viewModel!!.loadData()
    }


    private fun loadMoreData() {
        dataAdapter.loadMoreModule.isEnableLoadMore = false
        viewModel!!.loadMoreData()
    }

    abstract fun getAdapter(): BaseQuickAdapter<T, BaseViewHolder>

    open fun addHeaderView(adapter: BaseQuickAdapter<T,BaseViewHolder>){}

    override fun getLayoutId() = R.layout.common_activity_recycler_view

    override fun getPageTarget() = refreshLayout

    override fun reload() {
        viewModel!!.loadData()
    }
}